KD0YTE's Ham Radio, Linux, and other stuff.

Entries tagged "raspberry pi".

Temperature recording with the pi
17th April 2024

I have a semi sheltered area that I want to put some plants in but springtime in NE MO is hit and miss and I dont want to damage the plants.
I also have a garage that is unheated that i want to monitor how cold it gets this winter.
So I fixed up raspberry pi zeros to keep a temp monitor of those areas.

installed pi os 32 bit lite version

On it installed pyhon3-pip
sudo apt-get install python3-dev python3-pip
sudo python3 -m pip install --upgrade pip setuptools wheel

Also installed the ADafruit library for the DHT11
sudo pip3 install Adafruit_DHT

Then i came up with a script to read from the sensor and report temp in Fahrenheit and humidity
named it tempf.py

import Adafruit_DHT
import time

DHT_SENSOR = Adafruit_DHT.DHT11
DHT_PIN = 4

humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN)
tempf = (temperature*1.8)+32
print (today, ",", now, ",", tempf, " ,", humidity )
# print (humidity, "%,")

2nd found a script to output current date and time
named it dateit.py

  1. Import the 'datetime' module to work with date and time
    import datetime
  1. Get the current date and time
    now = datetime.datetime.now()
  1. Create a datetime object representing the current date and time
  1. Display a message indicating what is being printed
    print("Current date and time : ")
  1. Print the current date and time in a specific format
    print(now.strftime("%Y-%m-%d %H:%M:%S"))
  1. Use the 'strftime' method to format the datetime object as a string with the desired format

Then I wrote a bash script to call those 2 and send output to tempout.txt
temprec.sh

#!/bin/bash

python dateit.py >> ftemp.txt
python tempf.py >> ftemp.txt

Finally setup a crontab to run it every 5 minutes
crontab -e

*/5 * * * * /home/pi/temprec.sh

This could probably be done from 1 single python script but I am just not that good with python.
This works well. I might change to a longer interval for readings.

Output looks like this

2024-09-20 , 10:00:02.247606 , 78.80000000000001 , 64.0
2024-09-20 , 11:00:01.831673 , 82.4 , 61.0

Tags: linux, raspberry pi.
pi5 and nvme
24th April 2024

So once the raspberry pi became availabe I purchased one.
The first pi I ever owned was a raspberry pi 2, it was a slug bug except for the simplest of tasks.
I have owned several pi 3's, a pi zero and a pi zero 2 w, and several pi 4's.
The pi 3 was much more useful than the pi 2 and the pi 4 was approaching usable for desktop use.

When I purchased the pi 5 I had heard the hype about how it was much faster than the pi 4 but to be
honest I was dissapointed. It was a little faster but not much. and for some reason about every couple of
months it would bork the sd card and I would have to reinstall. I finally purchased one of those high
endurance sd cards thinking that they would last longer. However I decided about that time to try an
nvme hat from geekworm and a patriot gen3 nvme drive.
I set boot to do nvme first then booted from the nvme drive cloned from the sdcard. (BOOT_ORDER=0xf416)
in /boot/firmware/config.txt
Holy Cow! What a difference. suddenly my pi 5 was a speed demon. chrome which always took several seconds to
load was now popping right up.

I also enabled gen3 speeds which is expiramental. that makes it faster but not profoundly faster.

If you are running pi5 from an SD Card, I highly reccomend you try nvme.
I will come back here with stabilty results after using it for a few months.

Have fun!

Tags: linux, raspberry pi.
pi power off button
6th September 2024

You can make a shutdown button without the need for a running script by adding this to /boot/config.txt:

dtoverlay=gpio-shutdown

The default pin for the above is pin 5 (GPIO3). which is shorted to ground

If you plan to use I2C then you will need to change the shutdown pin to something else.
For example to change the shutdown pin from the default GPIO 3 to GPIO 26 (physical pin 37), add this to /boot/config.txt

dtoverlay=gpio-shutdown,gpio_pin=26

Tags: raspberry pi.
Temps to html
20th September 2024

So in an earlier post I talked about setting up a pi zero to do temp and humidity recording

Today we will take this output and convert it to an html file to post on a local webserver.
We will also automate this process and add an archiving operation in case we want to go back and look at past data

First you will need to install a couple of things on the pi.
1. a webserver. I am using nginx as it is light on resources and the pi zero isnt very fast
2. the pandas python library to do the converting so

sudo apt install nginx python3-pandas

Next make a python script. We will call it csvconv.py
nano csvconv.py

## Python program to convert
## CSV to HTML Table

import pandas as pd

## to read csv file named "temps.csv"
a = pd.read_csv("temps.csv")

## to save as html file
## named as "temps.html"
a.to_html("temps.html")

## assign it to a
## variable (string)
html_file = a.to_html()

press ctrl o to save and ctrl x to quit

next make a bash script to do the html conversion and add some header and footers

nano dohtmltemps.sh

#!/bin/bash

cp ftemp.txt temps.csv
rm temps.html
python csvconv.py

cat gheader.html temps.html gfooter.html > index.html
cp index.html /var/www/html/

ctrl o and ctrl x as before to save

(by default the /var/www/html folder is writable only as root you will need to get around this so the script can write the file.)
(you could chmod +w -R the folder but if you will have this webserver connected to the internet that is a security risk.)

the gheader.html and gfooter.html are simple html snippets to make the page more complete.

make yours as fancy as you like.

next I made a script to run weekly to archive the data for posterity.
nano arctmp.sh

#!/bin/bash

echo ftemp.txt > "ftemp-$(date +"%m-%d-%Y").txt"
echo temps.csv > "temps-$(date +"%m-%d-%Y").csv"

sleep 5
cp ftheader.txt ftemp.txt

This simply appends the date to the file name.

Finally you want to setup cron to record the temp hourly, make the html file nightly and archive the temp data weekly.
crontab -e to edit the crontab

Here is what I have in mine

*/60 * * * * /home/pi/temprec.sh

46 23 * * * /home/pi/dohtmltemps.sh

50 23 * * 0 /home/pi/arctmp.sh

enjoy!

Tags: linux, raspberry pi.

RSS Feed